using INTEX.DataBase;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
namespace WPFExcelReport
{
    public partial class VSYS_IOT_OVER_18339 : ViewModelBase
    {  
        public CollectionView vwSYS_IOT_OVER_18339
        {
            get
            {
                return (CollectionView)CollectionViewSource.GetDefaultView(SetData.Tables["SYS_IOT_OVER_18339"]);
            }
        }
        
        private DataRowView _dwSYS_IOT_OVER_18339 = null;
        public DataRowView dwSYS_IOT_OVER_18339
        {
            get
            {
                return _dwSYS_IOT_OVER_18339;
            }
            set
            {
                _dwSYS_IOT_OVER_18339 = value;
                OnPropertyChanged("dwSYS_IOT_OVER_18339", value);
            }
        } 
        private bool _IsSYS_IOT_OVER_18339; public bool IsSYS_IOT_OVER_18339 { get { return _IsSYS_IOT_OVER_18339; } set { _IsSYS_IOT_OVER_18339 = value; OnPropertyChanged("IsSYS_IOT_OVER_18339", value); } }
        
    
        public DelegateCommand QueryCommand { get; set; }
        public DelegateCommand DeleteCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public DelegateCommand CopyCommand { get; set; } 
        public VSYS_IOT_OVER_18339()
            : base()
        { 
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetData == null)
            {
                SetData = new SETINTEXDB();
            }
            if (SetProd == null)
            {
                SetProd = new SETINTEXDB();
            }
            TableName = "SYS_IOT_OVER_18339";
            TBMain = SetData.Tables[TableName];
            QueryCommand = new DelegateCommand(x => Query(null));
            DeleteCommand = new DelegateCommand(x => Delete(null));
            SaveCommand = new DelegateCommand(x => Save(null));
            CopyCommand = new DelegateCommand(x => Copy(null));
            if (app.User.Trim() == "ITXV")
            {
              
            }
        }
        ~VSYS_IOT_OVER_18339()
        {

        }

        public void LoadEnv()
        {
            try
            {
                IsSYS_IOT_OVER_18339 = true;
                DataSet SResult = new DataSet();
                DataSet SReturn = new DataSet();
                DataRow dwData = null;
                SetEnv.Clear(); 
                SetEnv.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override DataSet Query(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null; 
            try
            {
                dwData = dwInput;
                if (dwData == null)
                {
                    dwData = SetData.Tables["SYS_IOT_OVER_18339"].NewRow(); 
                } 
                SResult = QuerySYS_IOT_OVER_18339(dwData, 99); 
                if (SetData.Tables.Contains(TableName))
                {
                    SetData.Tables[TableName].Clear();
                }
                SReturn.Merge(SResult);
                SetData.Merge(SReturn);
                SetData.AcceptChanges();
                 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public override DataSet Delete(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null; 
            try
            { 
                dwData = dwInput; 
                if (dwData == null)
                {
                    MessageBox.Show("请选择需要删除的记录");
                    return SReturn;
                }
                DeleteSYS_IOT_OVER_18339(dwData, 1); 
                if(SetData.Tables[TableName].Rows.Count>1)
                {
                    string filter=" VAFMANDANT='"+dwData["VAFMANDANT"].ToString()+"' AND VAFKOLL='"+dwData["VAFKOLL"].ToString()+"' AND VAFART ='"+dwData["VAFART"].ToString()+"' AND VAFFARBE='"+dwData["VAFFARBE"].ToString()+"'";
                    DataRow[] rows = SetData.Tables[TableName].Select(filter);
                    foreach (DataRow row in rows)
                    {
                        row.Delete();
                    }
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public override DataSet Save(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null; 
            try
            {

                if (dwInput == null)
                {
                    MessageBox.Show("请选择需要保存的记录");
                    return SReturn;
                }
                SaveSYS_IOT_OVER_18339(dwInput, app.company); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public override DataSet Copy(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet(); 
            try
            {
                if (SelectedItem == null)
                    return SReturn;
                DataRow dwMain = ((DataRowView)SelectedItem).Row;
                if (dwMain == null)
                {
                    MessageBox.Show("请选择需要参考的记录");
                    return SReturn;
                } 
                DataRow dwNew = SetData.Tables["SYS_IOT_OVER_18339"].Rows.Add();
                dwNew.ItemArray = dwMain.ItemArray;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}
